import Crypt from "../../../utils/Crypt";
import PayModel from "../../../models/Pay";

const payModel = new PayModel()

Page({
  data: {
    amount: 0,
    sn: '',
    user_id: 0
  },
  onLoad: function (options) {
    let serial = options.serial
    serial = JSON.parse(serial)

    if (serial && serial.order_id) {
      payModel.getPayAmount({ order_id: serial.order_id }).then(response => {
        let { data } = response
        data = Crypt.parse(data)
        this.setData({ amount: data.amount, sn: data.sn, user_id: data.user_id })

        this.pay()
        // this.payThird()

        /*
          关于支付的代码:
            天物小程序是 第三方
            3000旅游那个是 小程序原生支付

            j2 是H5 支付
              注意 api 中 timeStamp 这个参数不能多传

            3000 是 小程序原生支付
              // 神奇的小程序支付, 这里的类型必须 是string
              前端:
                timeStamp: data.timestamp + '',
         */
      })
    }
  },

  payThird() {
    const data = this.data
    payModel.payPrepare({
      sn: data.sn,
      amount: data.amount,
      body: '购买商品',
      pay_type: 1,
      pay_type_text: '订单',
      user_id: data.user_id,
      // signature_url: ''
    }).then(data => {
      let { result } = data
      result = result.rc_Result
      wx.requestPayment({
        timeStamp: result.timeStamp,
        nonceStr: result.nonceStr,
        package: result.package,
        paySign: result.paySign,
        signType: result.signType,
        success: () => {
          // 调用接口成功
          wx.showToast({
            title: '支付成功',
            success: () => {
              setTimeout(() => {
                wx.switchTab({url: '/pages/center/index'})
              }, 1000)
            }
          })
        },
        fail: () => {
          // 调用接口失败
          wx.showToast({
            title: '支付失败',
            icon: 'error',
            success: () => {
              setTimeout(() => {
                wx.switchTab({url: '/pages/home/index'})
              }, 1000)
            }
          })
        }
      })
    })
  },

  pay() {
    const data = this.data
    payModel.payPrepareWx({
      sn: data.sn,
      amount: data.amount,
      body: '购买商品',
      pay_type: 1,
      pay_type_text: '订单',
      user_id: data.user_id,
      // signature_url: ''
    }).then(result => {
      const data = result
      console.log(data)
      // result = result.rc_Result
      wx.requestPayment({
        // timeStamp: result.timeStamp,
        // nonceStr: result.nonceStr,
        // package: result.package,
        // paySign: result.paySign,
        // signType: result.signType,
        // 神奇的小程序支付, 这里的类型必须 是string
        timeStamp: data.timestamp + '',
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.sign,
        signType: 'MD5',
        success: () => {
          // 调用接口成功
          wx.showToast({
            title: '支付成功',
            success: () => {
              setTimeout(() => {
                wx.switchTab({url: '/pages/center/index'})
              }, 1000)
            }
          })
        },
        fail: () => {
          // 调用接口失败
          wx.showToast({
            title: '支付失败',
            icon: 'error',
            success: () => {
              setTimeout(() => {
                wx.switchTab({url: '/pages/home/index'})
              }, 1000)
            }
          })
        }
      })
    })
  }
});
